package com.yanqu.road.dao.impl.recharge;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserCouponItem;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserCouponItemDaoImpl extends TempDataObjectDao<UserCouponItem> {

    public long getMaxItemId(long userId) {
        String sql = "select ifnull(max(itemId),0) as maxId from t_u_coupon where userId=?";
        ResultSet rs = null;
        CallableStatement call = null;
        long maxId = 0L;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                maxId = rs.getLong("maxId");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return maxId;
    }

    public Map<Long, UserCouponItem> getUserCouponItemMap(long userId, long selectTime) {
        String sql = "select * from `t_u_coupon` where `userId`=? and expirationTime>?";
        Map<Long, UserCouponItem> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            call.setLong(2, selectTime);
            rs = call.executeQuery();
            while (rs.next()) {
                UserCouponItem data = parse(rs);
                dataMap.put(data.getItemId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public int getCouponUsedCount(long userId) {
        String sql = "select count(*) from `t_u_coupon` where `userId`=? and isUse != 0";
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                return rs.getInt(1);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return 0;
    }

    @Override
    protected boolean updateData(UserCouponItem data) {
        String sql = "update `t_u_coupon` set goodsId=?,getTime=?,expirationTime=?,isUse=? where itemId=? and userId=?";
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getGoodsId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getGetTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getExpirationTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isUse()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getItemId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(sql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected boolean addData(UserCouponItem data) {
        String sql = "insert into `t_u_coupon`(itemId,userId,goodsId,getTime,expirationTime,isUse) values (?,?,?,?,?,?)";
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getItemId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGoodsId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getGetTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getExpirationTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isUse()));
        if (getDb().executeNoneQuery(sql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserCouponItem parseData(ResultSet rs) throws SQLException {
        UserCouponItem data = new UserCouponItem();
        data.setItemId(rs.getLong("itemId"));
        data.setUserId(rs.getLong("userId"));
        data.setGoodsId(rs.getInt("goodsId"));
        data.setGetTime(rs.getLong("getTime"));
        data.setExpirationTime(rs.getLong("expirationTime"));
        data.setUse(rs.getBoolean("isUse"));
        return data;
    }
}
